<?php
/**
 * 控制器跳转 Trait
 * @copyright 2020-2021 WillPHP
 * @author NoMind<24203741@qq.com/113344.com>
 * @version WillPHPv3
 * @since 2021-12-06
 */
namespace wiphp;
defined('WIPHP_URI') or die('Access Denied');
trait Jump {
	protected $isApi = false; //是否是api
	protected $codes = [200=>'请求成功',204=>'暂无记录',400=>'未知错误',401=>'请先登录',404=>'页面未找到',406=>'Token验证失败',500=>'服务器内部错误'];
	/**
	 * 返回josn数据
	 * @access protected
	 * @param int $code 状态码
	 * @param mixed $msg 提示信息
	 * @param array $data json数据
	 * @param string $url 跳转的URL地址
	 * @return void
	 */
	protected function json($code = 404, $msg = '', $data = [], $url = '') {			
		header('Content-type: application/json;charset=utf-8');
		header('Access-Control-Allow-Origin:*');
		header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Token');
		header('Access-Control-Allow-Methods:POST,GET');	
		$msg = (empty($msg) && isset($this->codes[$code]))? $this->codes[$code] : $msg;
		$status = ($code < 400)? 1 : 0;
		$result = ['code' => $code, 'status'=>$status, 'msg' => $msg, 'data' => $data, 'url'=> $url];
		exit(json_encode($result, JSON_UNESCAPED_UNICODE));
	}
	/**
	 * 操作成功跳转
	 * @access protected
	 * @param mixed $msg 提示信息
	 * @param string $url 跳转的URL地址
	 * @param array $data json数据
	 * @return void
	 */
	protected function success($msg = '', $url = null) {	
		$msg = (empty($msg) && isset($this->codes[200]))? $this->codes[200] : $msg;		
		$url = is_null($url)? '' : url($url);		
		if ($this->isAjax() || $this->isApi) {			
			$this->json(200, $msg, [], $url);
		}
		$result = ['code' => 200, 'status' => 1, 'msg' => $msg, 'url' => $url];
		view('public:jump', $result);
		exit();
	}
	/**
	 * 操作错误跳转
	 * @access protected
	 * @param mixed $msg 提示信息
	 * @param string $url 跳转的URL地址
	 * @param array $data json数据
	 * @return void
	 */
	protected function error($msg = '', $url = null) {	
		$msg = (empty($msg) && isset($this->codes[400]))? $this->codes[400] : $msg;	
		$url = is_null($url)? 'javascript:history.back(-1);' : url($url);
		if ($this->isAjax() || $this->isApi) {
			$this->json(400, $msg, [], $url);
		}
		$result = ['code' => 400, 'status' => 0, 'msg' => $msg, 'url' => $url];
		view('public:jump', $result);
		exit();
	}	
	/**
	 * 操作跳转合同并
	 * @access protected
	 * @param mixed $info 提示信息
	 * @param number $status 操作结果状态
	 * @param string $url 跳转的URL地址
	 * @param array $data json数据
	 * @return void
	 */
	protected function _jump($info, $status = 0, $url = null) {
		$msg = [];
		if (is_array($info)) {
			$msg = $info;
		} else {
			$msg[0] = $msg[1] = $info;
		}		
		if ($status) {			
			$this->success($msg[0], $url);
		} else {
			$this->error($msg[1], $url);
		}
	}
	/**
	 * URL重定向
	 * @access protected
	 * @param string $url 跳转的URL
	 * @param int $time 跳转时间
	 * @return void
	 */
	protected function _url($url, $time = 0) {
		$url = url($url);
		$time = max(0, intval($time));
		if ($time == 0) {
			header('Location:'.$url);
		} else {
			header("refresh:{$time};url={$url}");
		}
		exit();
	}
	/**
	 * 是否是Ajax提交
	 * @return bool
	 */
	protected function isAjax(){
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
			return true;
		}
		return $this->isApi;
	}
	/**
	 * 是否是POST提交
	 * @return bool
	 */
	protected function isPost() {
		return ($_SERVER['REQUEST_METHOD'] == 'POST' && (empty($_SERVER['HTTP_REFERER']) || preg_replace("~https?:\/\/([^\:\/]+).*~i", "\\1", $_SERVER['HTTP_REFERER']) == preg_replace("~([^\:]+).*~", "\\1", $_SERVER['HTTP_HOST']))) ? true : false;
	}
}